//
//  HMVisitorView.swift
//  weibo10
//
//  Created by mac on 15/10/17.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
//protocol HMVisitorViewDelegate: NSObjectProtocol {
//    func visitorViewDidRegistor()
//    func visitorViewDidLogin()
//}

class HMVisitorView: UIView {
    // 代理：weak引用的对象一旦被释放就会变为nil
    // weak var delegate: HMVisitorViewDelegate?
    // 监听方法
//    @objc private func registorClick(){
//        delegate?.visitorViewDidRegistor()
//    }
//    @objc private func loginClick(){
//        delegate?.visitorViewDidLogin()
//    }
    
    func setupInfo(imageName:String?, labelName:String){
        titleLabel.text = labelName

        guard let image = imageName else{
            setupAnimation()
            return
        }
        iconView.image = UIImage(named: imageName!)
        homeView.hidden = true
        // 将遮罩放大最后面
        sendSubviewToBack(backView)
    }
    private func setupAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 禁止使用sb/xib,如果想用sb、xib，将这句话注释
        //fatalError("init(coder:) has not been implemented")
        setupUI()
    }
    
    // MARK -懒加载控件
    // 图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    // 小房子
    private lazy var homeView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
   
    // 标签
    private lazy var titleLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    // 注册按钮
    lazy var registBtn:UIButton = UIButton(title: "注册",color:UIColor.orangeColor(), imageName: "common_button_white_disable")
   
    // 登录按钮
    lazy var loginBtn:UIButton = UIButton(title: "登录",color:UIColor.darkGrayColor(), imageName: "common_button_white_disable")
    
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
  
}
// MARK -设置界面
extension HMVisitorView{
    func setupUI(){
        // 1. 添加控件
        addSubview(iconView)
        addSubview(backView)
        addSubview(homeView)
        
        addSubview(titleLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        // 2. 自动布局
        // 默认是 true ，支持使用setFrame 的方式设置frame
        // false 支持使用自动布局的方式设置frame
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 标题
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 注册
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        // 登录
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: registBtn, attribute: .Right, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: registBtn, attribute: .Width, multiplier: 1.0, constant: 0))
        // 背景图片：另一种方法
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[back]-0-|", options: [], metrics: nil, views: ["back":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[back]-(btnHeight)-[reg]", options: [], metrics: ["btnHeight":-36], views: ["back":backView,"reg":registBtn]))
  
        // 背景颜色
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
//        // 3. 给"登录" "注册"按钮 添加监听方法
//        registBtn.addTarget(self, action: Selector("registorClick"), forControlEvents: UIControlEvents.TouchUpInside)
//        loginBtn.addTarget(self, action: Selector("loginClick"), forControlEvents: UIControlEvents.TouchUpInside)
    }
}
